package com.hsy.linemoney.apiservice;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.requests.RequestTask;
import com.hsy.linemoney.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;

/**
 * 或墙壁相关
 * Created by MartinKent on 2016/9/8.
 */
public class CurrencyService extends BasicService<JSONArray> {

//    获取货币列表
    public void getCurrency(final Callback<CurrencyListResult> callback, boolean... clearData) {
        if (null != clearData && clearData.length > 0 && clearData[0]) {
            DataHolder.mCurrencyListResult = null;
        }
        if (null != DataHolder.mCurrencyListResult) {
            if (null != callback) {
                callback.onSuccess(0, DataHolder.mCurrencyListResult);
            }
            return;
        }
        String api = "/LmReferenceServices-portlet.currency/get-currency";
        invoke(0, api, null, new Callback<JSONArray>() {

            @Override
            public void onSuccess(int what, JSONArray result) {
                if (null == DataHolder.mCurrencyListResult) {
                    try {
                        DataHolder.mCurrencyListResult = DPGsonUtil.fromJson(result.get(0).toString(), CurrencyListResult.class);
                        if (null != callback) {
                            callback.onSuccess(what, DataHolder.mCurrencyListResult);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        if (null != callback) {
                            callback.onError(what, 0, e.getLocalizedMessage(), e);
                        }
                    }
                } else {
                    callback.onSuccess(what, DataHolder.mCurrencyListResult);
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        });
    }

    public static class CurrencyListResult {
        private Status status;

        private List<Currency> value;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setCurrency(List<Currency> value) {
            this.value = value;
        }

        public List<Currency> getCurrency() {
            return this.value;
        }

        private String[] mCurrencyNameArray;

        public String[] getCurrencyNameArray() {
            if (null == mCurrencyNameArray) {
                mCurrencyNameArray = new String[getCurrency().size()];
                for (int i = 0; i < mCurrencyNameArray.length; i++) {
                    mCurrencyNameArray[i] = getCurrency().get(i).getDescription();
                }
            }
            return mCurrencyNameArray;
        }

        public String getCurrencyDescription(String currencyCd) {
            for (Currency item : getCurrency()) {
                if (item.getCurrencyCd().equals(currencyCd)) {
                    return item.getDescription();
                }
            }
            return "";
        }

        public static class Currency {
            private String description;

            private String currencyCd;

            public void setDescription(String description) {
                this.description = description;
            }

            public String getDescription() {
                return this.description;
            }

            public void setCurrencyCd(String currencyCd) {
                this.currencyCd = currencyCd;
            }

            public String getCurrencyCd() {
                return this.currencyCd;
            }

        }

        public static class Status {
            private String message;

            private String code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public String getCode() {
                return this.code;
            }

        }
    }
}
